package com.hbnu.controller;

import com.hbnu.pojo.Door;
import com.hbnu.service.DoorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class DoorsController {
    @Autowired
    private DoorsService doorsService;

    @RequestMapping("/doorList")
    public String getAllDoors(Model model) {
        List<Door> doorList = doorsService.getDoors();
        model.addAttribute("doors",doorList);
        return "door_list";
    }

    @RequestMapping("/doorInfo")
    public String getDoorById(Integer id,Model model){
        Door door = doorsService.getDoorById(id);
        model.addAttribute("door",door);
        return "door_update";
    }
    @RequestMapping("/deleteDoor")
    public String deleteDoorById(int id, Model model){
        int flag = doorsService.deleteDoorById(id);
        return "redirect:/doorList";
    }
    @RequestMapping("/addDoor")
    public String addDoor(Door door,Model model){
        int flag = doorsService.addDoor(door);
        return "redirect:/doorList";
    }
    @RequestMapping("/updateDoor")
    public String updateDoor(Door door,Model model){
        int flag = doorsService.updateDoor(door);
        return "redirect:/doorList";
    }
}
